'''
Created on May 12, 2010

This Module includes the required functions to handle the configuration of
the SSH Server.
'''

import augeas

class SSH(object):
    '''
    SSH Configuration Class. Used to query or set options on the SSH daemon configuration file.
    '''

    def __init__(self):

        self.augeasObj = augeas.Augeas()
        self.supportedOptions = ['PasswordAuthentication', 'PermitEmptyPasswords', \
                                'PermitRootLogin', 'PubkeyAuthentication', \
                                'PrintMotd', 'UsePAM', 'ListenAddress', 'Port', \
                                'Protocol', 'X11Forwarding', 'SyslogFacility' ]

    def getOptions(self):
        '''
        getOptions()
            return a list of tuples contains pairs of (option, value).
            Note: only supported modules can be queried.
        '''

        # Get a list of all the options in /etc/ssh/ssh_config
        keyList = self.augeasObj.match('/files/etc/ssh/sshd_config/*')

        optionsList = []

        # Add only options to the new list (omitting comments)
        for i in range(len(keyList)) :
            if keyList[i].split('/')[-1].startswith('#') :
                continue

            else :
                optionsList.append(keyList[i])

        configDictionary = {}

        # Add only the supported options to a list of tuples of (option, value) pairs.
        for key in optionsList :
            option = key.split('/')[-1]

            if option in self.supportedOptions :
                value = self.augeasObj.get(key)
                configDictionary[option] = value

        return configDictionary

    def setOption(self, option, value):
        '''
        setOption(option, value)
            Set the value of the selected option on the configuration file.
            Returns True if the option changed successfully, False otherwise.
            Note: only supported modules can be modified. 
        '''

        # Only accept supported options
        if option not in self.supportedOptions :
            return False

        self.augeasObj.set('/files/etc/ssh/sshd_config/' + option, value)


    def saveOptions(self, optionsDictionary):
        '''
        setOptions(optionsDictionary)
            Take a dictionary of "option:value" sets, and set each option
            to the specified value, the save the configuration.
        '''

        for option in optionsDictionary :
            self.setOption(option, optionsDictionary[option])

        if self.save() :
            return True
        else :
            return False

    def save(self):
        '''
        save()
            Save the modifications to the configuration file.
        '''

        # Try to save the changes to the configuration file.
        try :
            self.augeasObj.save()

        except Exception :
            return False

        # Return True if everything worked.
        return True


def main():

    x = SSH()

    print x.getOptions()

    if x.setOption('PrintMotd', 'yes') :
        print 'option saved'
    else :
        print 'error saving the option'


if __name__ == "__main__" :
    pass
    #main()
